from typing import List, Optional
from sqlalchemy.orm import Session

from app.crud.base import CRUDBase
from app.db.models import Schedule
from app.schemas.schedule import ScheduleCreate, ScheduleUpdate

class CRUDSchedule(CRUDBase[Schedule, ScheduleCreate, ScheduleUpdate]):
    def get_by_spider_id(self, db: Session, *, spider_id: str) -> List[Schedule]:
        return db.query(Schedule).filter(Schedule.spider_id == spider_id).all()
    
    def get_active_schedules(self, db: Session) -> List[Schedule]:
        return db.query(Schedule).filter(Schedule.enabled == True).all()
    
    def delete_by_spider_id(self, db: Session, *, spider_id: str) -> None:
        db.query(Schedule).filter(Schedule.spider_id == spider_id).delete()
        db.commit()

crud_schedule = CRUDSchedule(Schedule)